package pers.mobian.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.mobian.springcloud.pojo.Dept;

import java.util.List;

@RestController
public class DeptConsumerController {

    //次控制器类来自消费者，访问对应的端口，就能够访问到指定的提供者的信息
    //RestTemplate  注册到Spring容器中，供我们调用对应的方法即可
    @Autowired
    private RestTemplate restTemplate;

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    //由于使用的负载均衡，所以访问的应该是对应的服务，而非是具体的端口
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, boolean.class);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }


    @RequestMapping("/consumer/dept/list")
    public List<Dept> list() {
        //getForObject：表示对应的访问方式为get
        //参数列表：访问的提供者的路径，以及返回的类型
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

}
